<%--
  Created by IntelliJ IDEA.
  User: 86183
  Date: 2022/5/5
  Time: 13:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>客服</title>
    <link rel="shortcut icon" href="#"/>
    <style>
        body{
            color: white;
            background-color: black;
        }
    </style>
</head>
<body id="complaintBody">
    请选择投诉类型:<br>
    <input type="radio" name="complaint" value="用户投诉店铺" >用户投诉店铺
    <input type="radio" name="complaint" value="用户投诉平台" >用户投诉平台
    <input type="radio" name="complaint" value="商家投诉用户">商家投诉用户
    <input type="radio" name="complaint" value="商家投诉平台">商家投诉平台
    <input type="radio" name="complaint" value="封禁投诉">封禁投诉<br>
    投诉店铺或用户时请输入其全名,投诉平台时请输入'平台':<input id="complaintObj"><br>
    选择'封禁投诉'时请输入您的全昵称<input id="name"><br>
    请输入投诉原因:<input type="text" id="reason"><br>
    <button id="over" onclick="over()">提交</button>
    <button id="goBack" onclick="goBack()" >返回</button>
</body>
<script>
    function getCookie(name) {
        if (document.cookie.length > 0) {
            let start = document.cookie.indexOf(name + "=");
            if (start != -1) {
                start = start + name.length + 1;
                let end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                return decodeURI(document.cookie.substring(start, end));
            }
        }
        return null;
    }
    let username=getCookie("name")
    let id=getCookie("id");
    function goBack(){
        let role=getCookie("role");
        if(role=='商家'){
            window.location="Seller.jsp"
        }else{
            window.location="index.jsp"
        }
    }
    function over(){
        let complaintTypes=document.getElementsByName("complaint");
        let type;
        console.log(complaintTypes.length);
        for(let i=0;i<complaintTypes.length;i++){
            if(complaintTypes[i].checked){
                type=complaintTypes[i].value;
                console.log(type)
            }
        }
        console.log(type);
        let reason=document.getElementById("reason").value;
        console.log(reason);
        let complaintObj=document.getElementById("complaintObj").value;
        console.log(complaintObj);
        console.log(getCookie("id"));
        let updateTime=new Date().toUTCString();
        console.log(updateTime);
        let name=document.getElementById("name").value;
        console.log(name);
        if(type!='' && complaintObj!='' && reason!=''){
            let notice={
                "recipientId":id,
                "content":"投诉类型:"+type+" 投诉人:"+username+" 被投诉对象:"+complaintObj+" 原因:"+reason,
                "state":true,
                "type":"complaint",
                "mode":"complaint"
            }
            if(type=='封禁投诉'){
                if(name==''){alert("您未输入您的全名!");return;}
                notice={
                    "content":"投诉类型:"+type+" 投诉人:"+name+" 被投诉对象:"+complaintObj+" 原因:"+reason,
                    "state":true,
                    "mode":"complaint",
                    "type":"ban",
                    "name":name
                }
            }
            console.log(JSON.stringify(notice))
            let xhr=new XMLHttpRequest();
            xhr.open("post","http://localhost:80/notice",true);
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
            xhr.responseType='json';
            xhr.send("json="+JSON.stringify(notice));
            xhr.onreadystatechange=function(){
                if(xhr.status==200 && xhr.readyState==4){
                    let data = xhr.response;
                    console.log(data);
                    if(data["code"]==404){
                        console.log(data.state);
                        alert("操作失败,请稍后再试!");
                        window.location="index.jsp";
                    }else{
                        console.log(JSON.parse(data.data));
                        alert(data.state)
                        window.location="index.jsp";
                    }
                }
            }
        }else{
            alert("您的投诉信息不完整!")
        }
    }
</script>
</html>
